<template>
	<view class="contain">
		
		<view class="tablist">
			<u-tabs :list="list1" @click="click" :activeStyle="sactiveStyle"></u-tabs>
		</view>
		
		<view class="tables" style="margin-top:10px;">
			<u-row customStyle="margin-bottom: 10px">
				<u-col span="2" justify="center" align="center" textAlign="center">
					<view class="mid blod">姓名</view>
				</u-col>
				<u-col span="3" justify="center" align="center" textAlign="center">
					<view class="mid blod">上课时间</view>
				</u-col>
				<u-col span="2" justify="center" align="center" textAlign="center">
					<view class="mid blod">签到</view>
				</u-col>
				<u-col span="1" justify="center" align="center" textAlign="center">
					<view class="mid blod">积分</view>
				</u-col>
				<u-col span="2" justify="center" align="center" textAlign="center">
					<view class="mid blod">剩余课</view>
				</u-col>
				<u-col span="2" justify="center" align="center" textAlign="center">
					<view class="mid blod">老师</view>
				</u-col>
			</u-row>
		</view>
		
		<block v-if="hasLogin">
				<view v-if="myCourses.data[0]!=undefined" class="tables" style="margin-top:10px;" v-for="item in myCourses.data">
					<u-row customStyle="margin-bottom: 10px">
						<u-col span="2" justify="center" align="center" textAlign="center">
							<view class="small">{{item.cuser}}</view>
						</u-col>
						<u-col span="3" justify="center" align="center" textAlign="center">
							<view class="small">{{formatDate(item.signdate)}}</view>
						</u-col>
						<u-col span="2" justify="center" align="center" textAlign="center">
							<view class="small">{{item.signtext}}</view>
						</u-col>
						<u-col span="1" justify="center" align="center" textAlign="center">
							<view class="small">{{item.jifen}}</view>
						</u-col>
						<u-col span="2" justify="center" align="center" textAlign="center">
							<view class="small">{{item.beizhu}}</view>
						</u-col>
						<u-col span="2" justify="center" align="center" textAlign="center">
							<view class="small">{{item.teachername}}</view>
						</u-col>
					</u-row>
				</view>
				<view class="courses" v-else>
					<view style="margin-top:60rpx;"><u-empty mode="data" text="暂无相关数据,若绑定错误,请联系老师修改"></u-empty></view>
				</view>			
		</block>
		<block v-else>
			<view class="courses" @click="GoUrl('/pages/login/login')">
				<view style="margin-top:60rpx;"><u-empty mode="permission" text="请登录后访问"></u-empty></view>
			</view>	
		</block>

		
		
	</view>
</template>

<script setup lang='ts'>
import { ref,reactive } from 'vue'
import {  onLoad,onShow,onReachBottom } from '@dcloudio/uni-app'
// import { bus } from '@/utils/bus'
import { GoUrl } from '@/utils/utils'
import { userStore } from '@/stores/userStore'
let store = userStore();	
let { hasLogin,userData } = userStore();
let studentName=ref("");
let season=ref("春季");
let pageNum=ref(1);
let pageSize=ref(18);
let totalData=ref(0);

let myCourses=reactive({data:[]});

const sactiveStyle=reactive({
	"color":"#000",
	"fontSize":"18px",
	"fontWeight":"700"
});



let list1=reactive([
	{
		name: '春季班',
	}, {
		name: '夏季班',
	},{
		name: '秋季班'
	},{
		name: '冬季班'
	}
]);

const click=(e:any)=>{
	console.log("选择",e);
	season.value=e.name.replace("班","");
	myCourses.data=[];
	getData(); //获取数据
}

onLoad(()=>{
	getData(); //获取数据
});

onShow(()=>{
	if(!hasLogin){
		uni.$tools.Toast("请登录");
		setTimeout(()=>{
			uni.$tools.GoUrl("/pages/login/login");
		},1200);
	}	
});

const getData=()=>{
	if(userData.userinfo!=null && userData.userinfo!=''){
		studentName.value=userData.userinfo.username;
		console.log("当前学生姓名",studentName.value);
		FindStudentCourses(studentName.value);
	}	
}

const formatDate=(inputDate:string)=>{
	//let str=inputDate.replace(/-/gi,".");
	let arr=[];
	let format=(datenum:string)=>{
		if(parseInt(datenum)<10){
			return datenum.substring(1);
		}else{
			return datenum;
		} 
	}
	if(inputDate.indexOf("-")!=-1){
		arr=inputDate.split("-");
		let year=arr[0];
		let month=format(arr[1]);
		let day=format(arr[2]);
		return year+"."+month+"."+day;
	}
	return inputDate;
}

const FindStudentCourses=(studentName:string)=>{
	if(studentName!=''){
		let paraData={
			coursename:season.value,
			cuser:studentName,//"孙诺一"
		}
		console.log("查找姓名参数:",paraData);
		uni.$tools.Reqeust('/api/getStudentClocking?pageNum='+pageNum.value+'&pageSize='+pageSize.value,paraData,'GET').then((res:any)=>{
			console.log('课程返回:==>',res);
			if (res.data.code == 200) {
				uni.hideLoading();
				totalData.value=res.data.total;
				res.data.rows.forEach(item=>{
					console.log(item);
					myCourses.data.push(item);
				});
			}else{
				uni.hideLoading();
			}
		})		
	}	
}
onReachBottom(()=>{
	console.log("到底了...加载数据");
	if(myCourses.data.length<totalData.value){
		pageNum.value++;
		FindStudentCourses(studentName.value);
	}else{
		uni.$tools.Toast("别拉了,我也是有底线的~");
	}
})
</script>

<style lang='scss' scoped>
	@import "mycourse.scss";
</style>